package com.example.gkcircle.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.example.gkcircle.BaseActivity;
import com.example.gkcircle.R;
import com.example.gkcircle.guide.GuideActivity;
import com.example.gkcircle.utils.ActivityUtil;
import com.example.gkcircle.utils.CommonUtil;
import com.example.gkcircle.utils.MyAlertView;
import com.example.gkcircle.utils.MySVProgressHUD;
import com.example.gkcircle.utils.StringUtil;
import com.example.gkcircle.utils.ToastFactory;
import com.example.gkcircle.widget.EditTextWithDelete;

/**
 * 登录页
 * Created by Jehu on 16/3/25.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener{


    private Button mLoginBtn;
    private EditTextWithDelete mPhoneEdit;
    private EditTextWithDelete mPswEdit;
    private LinearLayout mBackLayout;
    private TextView mRegisterTv;
    private TextView mForgetPswTv;



    @Override
    protected void initView() {
        setContentView(R.layout.login_activity);
        this.mLoginBtn=(Button)this.findViewById(R.id.login_btn);
        this.mPhoneEdit=(EditTextWithDelete)this.findViewById(R.id.login_phone_edit);
        this.mPswEdit=(EditTextWithDelete)this.findViewById(R.id.login_psw_edit);
        this.mBackLayout=(LinearLayout)this.findViewById(R.id.login_back_layout);
        this.mRegisterTv=(TextView)this.findViewById(R.id.login_register_tv);
        this.mForgetPswTv=(TextView)this.findViewById(R.id.login_forget_psw_tv);
    }

    @Override
    protected void initListener() {
        mLoginBtn.setOnClickListener(this);
        mBackLayout.setOnClickListener(this);
        mRegisterTv.setOnClickListener(this);
        mForgetPswTv.setOnClickListener(this);
    }

    @Override
    protected void setData() {
        Bundle bundle=getIntent().getExtras();
        if(getIntent().getExtras()!=null){
            String phone=bundle.getString("phone");
            String password=bundle.getString("password");
            if(phone!=null && password!=null){
                mPhoneEdit.setText(bundle.getString("phone"));
                mPswEdit.setText(bundle.getString("password"));
                login(phone, password);
            }
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent =new Intent(this,RegisterActivity.class);
        Bundle bundle=new Bundle();
        switch (v.getId()){
            case R.id.login_back_layout:
                finish();
                break;
            case R.id.login_btn:
                String phone=mPhoneEdit.getText().toString().trim();
                String psw=mPswEdit.getText().toString().trim();
                if(phone.isEmpty() || psw.isEmpty()){
                    return;
                }
                CommonUtil.hideSoftInputView(this);
                login(phone, psw);
                break;
            case R.id.login_register_tv:
                bundle.putInt("flag", 0);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.login_forget_psw_tv:
                bundle.putInt("flag",1);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
        }
    }


    private void login(String phone,String psw){
        AVUser.logInInBackground(phone, psw, new LogInCallback<AVUser>() {
            @Override
            public void done(AVUser avUser, AVException e) {
                if(e==null){
                    if(null!=getIntent().getExtras()){
                        LoginActivity.this.setResult(RESULT_OK);
                    }
                    finish();
                }else{
                    final String status;
                    switch (e.getCode()){
                        case 211:
                            status="用户不存在";
                            break;
                        case 210:
                            status="密码错误";
                            break;
                        default:
                            status=getResources().getString(R.string.connection_error);
                            break;
                    }
                    MyAlertView.showAlertViewWithOK(LoginActivity.this,getString(R.string.login_failure),status,"好");
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
        {
            if(MyAlertView.alertView!=null && MyAlertView.alertView.isShowing()){
                MyAlertView.alertView.dismiss();
                return false;
            }
        }

        return super.onKeyDown(keyCode, event);

    }
}
